package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.result.R;
import com.atguigu.eduservice.constant.CourseStatus;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kabuda
 * @create 2023-05-05 10:31
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    @PostMapping
    public R addCourse(@RequestBody CourseInfoVo courseInfoVo){
        String id=eduCourseService.saveCourse(courseInfoVo);
        return R.ok().data("id",id);
    }


    @GetMapping("{id}")
    public R getCourse(@PathVariable("id") String id){
       CourseInfoVo courseInfoVo=eduCourseService.getCourse(id);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    @PutMapping
    public R updateCourse(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourse(courseInfoVo);
        return R.ok();
    }

    @PutMapping("{id}")
    public R publishCourse(@PathVariable("id") String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus(CourseStatus.Normal);
        boolean b = eduCourseService.updateById(course);
        if(!b){
            throw new MyException(20001,"上线课程失败");
        }
        return R.ok();
    }
    @GetMapping("courseInfo/{id}")
    public R courseInfo(@PathVariable("id") String id){
        CoursePublishVo coursePublishVo=eduCourseService.courseInfo(id);
        return R.ok().data("course",coursePublishVo);
    }

    @PostMapping("{page}/{limit}")
    public R pageQuery(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody CourseQuery courseQuery){
        R r=eduCourseService.pageQuery(page,limit,courseQuery);
        return r;
    }
    @DeleteMapping("{courseId}")
    public R removeCourseInfo(@PathVariable("courseId") String courseId){
        eduCourseService.removeCourseInfo(courseId);
        return R.ok();
    }


}
